<?php
/**
 * Created by PhpStorm.
 * User: 钟旺
 * Date: 2019/3/25
 * Time: 14:26
 */

namespace App\Validator;


use App\Exceptions\BaseException;

use App\Exceptions\ValidatorException;
use Illuminate\Support\Facades\Validator;

class BaseValidator extends Validator
{
    protected $scene = [];
    protected $msg = [];

    public function __construct()
    {

    }

    public function checkData($data, $sceneName)
    {
        if (!$this->scene || !is_array($this->scene[$sceneName])) {
            throw new BaseException(['msg' => 'scene参数不合法']);
        }
        if (!$this->msg || !is_array($this->msg)) {
            throw new BaseException(['msg' => 'msg参数不合法']);
        }

        $validator = self::make($data, $this->scene[$sceneName], $this->msg);//data 前端传递过来的参数，scene验证场景，$msg输出相关信息
        if (!($validator->passes())) {
            throw new ValidatorException(['msg' => current($validator->errors()->messages())[0]]);  //不通过则抛出异常
        }
        return true;
    }
}